﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MP_ATM
{
    public partial class MainAdmin : Form
    {
        private SqlConnection con = null;

        public MainAdmin()
        {
            InitializeComponent();
        }

        private void MainAdmin_Load(object sender, EventArgs e)
        {
            this.groupcaptaikhoan.Show();
            this.AcceptButton = btncaptaikhoan;
            string strCon = "Data Source=THANHTU-VAIO\\SQLEXPRESS;Initial Catalog=MP_ATM;Integrated Security=True";
            this.con = new SqlConnection(strCon);
            textMatk.Text = this.LoadID_User().ToString();
            this.AcceptButton = btncaptaikhoan;
            groupcaptaikhoan.BringToFront();
            groupcaptaikhoan.Show();
        }
        private void btncaptaikhoan_Click(object sender, EventArgs e)
        {
            groupcaptaikhoan.BringToFront();
            groupcaptaikhoan.Show();
            textMatk.Text = this.LoadID_User().ToString();
            this.LoadDataUser1();
            this.textpin.Focus();
        }

        private int LoadID_User()
        {
            int result = 0;
            SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            command.CommandText = "select max(matk) matk from users";
            DataTable dt_loaduser = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt_loaduser);
            if (dt_loaduser.Rows.Count > 0)
            {
                try
                {
                    result = int.Parse(dt_loaduser.Rows[0]["matk"].ToString()) + 1;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            return result;
        }

        private void LoadDataUser1()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            command.CommandText = "select * from vw_Users";
            DataTable dt_loaduser = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt_loaduser);
            dgvCaptk.DataSource = dt_loaduser;
        }

        private void LoadDataUser1(string matk, string cmnd)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            command.CommandText = "select * from vw_Users where [Mã Tài Khoản] like @matk and [Chứng minh ND] like @cmnd";
            command.Parameters.Add("@matk", SqlDbType.NVarChar, 50).Value = "%" + matk + "%";
            command.Parameters.Add("@cmnd", SqlDbType.NVarChar, 50).Value = "%" + cmnd + "%";
            DataTable dt_loaduser = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt_loaduser);
            dgvCaptk.DataSource = dt_loaduser;
        }

        private void LoadDataUser()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            command.CommandText = "select * from vw_Users";
            DataTable dtUser = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dtUser);
            dgvUser.DataSource = dtUser;
        }

        private void LoadDataUser(string matk, string cmnd)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            command.CommandText = "select * from vw_Users where [Mã Tài Khoản] like @matk and [Chứng minh ND] like @cmnd";
            command.Parameters.Add("@matk", SqlDbType.NVarChar, 50).Value = "%" + matk + "%";
            command.Parameters.Add("@cmnd", SqlDbType.NVarChar, 50).Value = "%" + cmnd + "%";
            DataTable dtUser = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dtUser);
            dgvUser.DataSource = dtUser;
        }

        private void btnsuataikhoan_Click(object sender, EventArgs e)   
        {
            groupsutaikhoan.BringToFront();
            groupsutaikhoan.Show();
            textMaPIN.Focus();

            this.LoadDataUser();
        }

        private void btnnaptien_Click(object sender, EventArgs e)
        {
            groupNaptien.BringToFront();
            groupNaptien.Show();
            textMatknap.Clear();
            textMatknap.Focus();
            numericUpDown1.ResetText();
        }

        private void btndangxuat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn đăng xuất không ?", "Đăng Xuất", MessageBoxButtons.OKCancel)== DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void MainAdmin_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgvUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textTaiKhoan.Text = dgvUser.Rows[e.RowIndex].Cells[0].Value.ToString();
                textMaPIN.Text = dgvUser.Rows[e.RowIndex].Cells[1].Value.ToString();
                textHoTen.Text = dgvUser.Rows[e.RowIndex].Cells[2].Value.ToString();
                dateTimePickerNgaySinh.Value = (DateTime)dgvUser.Rows[e.RowIndex].Cells[3].Value;
                textDiaChi.Text = dgvUser.Rows[e.RowIndex].Cells[4].Value.ToString();
                textSDT.Text = dgvUser.Rows[e.RowIndex].Cells[5].Value.ToString();
                textCMND.Text = dgvUser.Rows[e.RowIndex].Cells[6].Value.ToString();
                cmbGioiTinh.Text = dgvUser.Rows[e.RowIndex].Cells[7].Value.ToString();
                textSoTien.Text = dgvUser.Rows[e.RowIndex].Cells[8].Value.ToString();
                cmbTrangThai.Text = dgvUser.Rows[e.RowIndex].Cells[9].Value.ToString();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
              SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            command.CommandText = "update users set mapin = @mapin, hoten = @hoten, ns = @ns, diachi = @diachi, sdt = @sdt," +
            "cmt = @cmt, gioitinh = @gioitinh, mney = @mney, trangthai = @trangthai" +
            " where matk = @matk;";
            command.Parameters.Add("@mapin", SqlDbType.NVarChar, 50).Value = textMaPIN.Text;
            command.Parameters.Add("@hoten", SqlDbType.NVarChar, 50).Value = textHoTen.Text;
            command.Parameters.Add("@ns", SqlDbType.DateTime).Value = dateTimePickerNgaySinh.Value;
            command.Parameters.Add("@diachi", SqlDbType.NVarChar, 50).Value = textDiaChi.Text;
            command.Parameters.Add("@sdt", SqlDbType.NVarChar, 50).Value = textSDT.Text;
            command.Parameters.Add("@cmt", SqlDbType.NVarChar, 50).Value = textCMND.Text;
            command.Parameters.Add("@gioitinh", SqlDbType.Bit).Value = cmbGioiTinh.SelectedIndex;
            command.Parameters.Add("@mney", SqlDbType.Decimal).Value = textSoTien.Text;
            command.Parameters.Add("@trangthai", SqlDbType.Bit, 50).Value = cmbTrangThai.SelectedIndex;
            command.Parameters.Add("@matk", SqlDbType.NVarChar, 50).Value = textTaiKhoan.Text;
            this.con.Open();
            try
            {
                command.ExecuteNonQuery();
                this.LoadDataUser();
                MessageBox.Show("Bạn đã sửa thành công");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.con.Close();
            }

            
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void btnTimKiemUser_Click(object sender, EventArgs e)
        {
            this.LoadDataUser(textTaiKhoanTK.Text.Trim(), textCMNDTK.Text.Trim());
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
           SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            command.CommandText = "insert into users(matk,mapin,hoten,ns,diachi,sdt,cmt,mney,gioitinh,trangthai)"
                        + " values (@matk,@mapin,@hoten,@ns,@diachi,@sdt,@cmt,@mney,@gioitinh,@trangthai);";
            command.Parameters.Add("@matk", SqlDbType.NVarChar, 50).Value = textMatk.Text;
            command.Parameters.Add("@mapin", SqlDbType.NVarChar, 50).Value = textpin.Text;
            command.Parameters.Add("@hoten", SqlDbType.NVarChar, 50).Value = textTen.Text;
            command.Parameters.Add("@ns", SqlDbType.DateTime).Value = dateTimePicker2.Value;
            command.Parameters.Add("@diachi", SqlDbType.NVarChar, 50).Value = textDC.Text;
            command.Parameters.Add("@sdt", SqlDbType.NVarChar, 50).Value = textSDT.Text;
            command.Parameters.Add("@cmt", SqlDbType.NVarChar, 50).Value =  textCMT.Text;
            command.Parameters.Add("@gioitinh", SqlDbType.Bit).Value = cmbGT.SelectedIndex;
            command.Parameters.Add("@mney", SqlDbType.Decimal).Value = textTien.Text;
            command.Parameters.Add("@trangthai", SqlDbType.Bit, 50).Value = cmbTThai.SelectedIndex;
            this.con.Open();
            try
            {
                command.ExecuteNonQuery();
                MessageBox.Show("Bạn đã thêm thành công");
                this.LoadDataUser1();
                textMatk.Text = this.LoadID_User().ToString();
                textpin.Clear();
                textSodt.Clear();
                textTen.Clear();
                textTien.Clear();
                textDC.Clear();
                textCMT.Clear();
                cmbGT.Text = "";
                cmbTThai.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.con.Close();
            }
        }

        private void cmbTrangThai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.LoadDataUser1(textTKcap.Text.Trim(), textCMTcap.Text.Trim());
        }

        private void btnXoatk_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa Tài khoản này không ?", "Thông báo XÓA", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.con.Open();
                    SqlCommand command = new SqlCommand();
                    command.Connection = con;
                    command.CommandType = CommandType.Text;
                    if (dgvCaptk.SelectedRows.Count > 0)
                    {
                        try
                        {
                            string stmatk = dgvCaptk.SelectedRows[0].Cells[0].Value.ToString();
                            command.CommandText = System.String.Concat("Delete gdrut where matk='" + stmatk + "'");
                            int count = command.ExecuteNonQuery();
                            command.CommandText = System.String.Concat("Delete gdchuyen where matk='" + stmatk + "'");
                            count += command.ExecuteNonQuery();
                            command.CommandText = "Delete users where matk = '" + stmatk + "';";
                            count += command.ExecuteNonQuery();
                            MessageBox.Show("Đã xóa xong");
                            this.LoadDataUser1();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            this.con.Close();
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi không xóa được !!!", "Thông Báo");
            }
        }

        private void btnNap_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = this.con;
            DataTable dtUser = new DataTable(), dtUserChuyen = new DataTable();
            command.CommandText = "select * from users where matk = @matk";
            command.Parameters.Add("@matk", SqlDbType.NVarChar, 50).Value = textMatknap.Text;
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dtUser);
            this.con.Open();

            try
            {
                decimal taikhoan = decimal.Parse(dtUser.Rows[0]["mney"].ToString()) + numericUpDown1.Value;
                command = new SqlCommand();
                command.Connection = this.con;
                command.CommandText = "update users set mney = @mney where matk = @matk";
                command.Parameters.Add("@mney", SqlDbType.Decimal).Value = taikhoan;
                command.Parameters.Add("@matk", SqlDbType.NVarChar, 50).Value = textMatknap.Text;
                command.ExecuteNonQuery();
                MessageBox.Show("Nạp Thành Công !!!");
                textMatknap.Clear();
            }
            catch (Exception)
            {
                if (MessageBox.Show("Tài khoàn bạn nhập không đúng!!!", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    textMatknap.Focus();
                    textMatknap.Clear();
                }
            }
            finally
            {
                this.con.Close();
            }
            
        }
    }
}
  